home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / graphics / sprite10.arj / SPRTEST.C < prev    next >
Text File  |  1994-02-07  |  4KB  |  223 lines

  1. #include <stdlib.h>;
  2. #include <stdio.h>;
  3. #include "vga18.h";
  4.  
  5. #define uchar unsigned char
  6. #define VGA256 19
  7. #define VGA18 18
  8. #define TEXTMODE 3
  9.  
  10. FILE *FP;
  11. int *AUTOMATON;              /* Pointer on buffer for automaton */
  12. char *BLOCKSAVE;             /* Pointer on buffer for screen block */
  13. int AUTMAX;                  /* Number of entries in automaton */
  14. int SPRMAX;                  /* Number of sprites in table */
  15.  
  16. /*
  17.   you don't need to have the three following lines if
  18.   you include MCGALIB.OBJ into the project...
  19. */
  20.  
  21. unsigned long *SPVECTOR;
  22. char  *SPCODE;
  23. unsigned int VGASCREEN=0xA000;
  24.  
  25.  
  26. void changext(char *name,char *ext);
  27. void putsxyf(int x,int y,char *fmt, ... );
  28. int bload(char *fname,char *adr,int siz);
  29. int getcode(int n);
  30. void putpic(int num,int x,int y);
  31. void putscenery(int num,int x,int y);
  32. int autload(char *name);
  33. int sprload(char *name);
  34. int spralloc();
  35.  
  36. /* Function to add or replace extension of filenames */
  37.  
  38. void changext(char *name,char *ext)
  39. {
  40. int l;
  41.  
  42. l=min( strcspn(name,".") , 8);
  43. *(name+l)=0;
  44. strcat(name,ext);
  45. }
  46.  
  47. /* Displays a formatted text string */
  48.  
  49. void putsxyf(int x,int y,char *fmt, ... )
  50. {
  51.  char STRBUF19[81];
  52.  
  53.  va_list  argptr;
  54.  va_start( argptr, format );
  55.  vsprintf(STRBUF19, fmt, argptr );
  56.  putsxy(x,y,STRBUF19);
  57.  va_end( argptr );
  58. }
  59.  
  60. /* Function to read the graph of an automaton */
  61.  
  62. int autread(int i)
  63. {
  64. return ( *(AUTOMATON+i));
  65. }
  66.  
  67. /* Function to get a sprite type code */
  68.  
  69. int getcode(int n)
  70. {
  71. char *adr=getspadr18(n);  return((int)*(adr+4));
  72. }
  73.  
  74. /* Error message for loading function */
  75.  
  76. int puterr(char *name)
  77. {
  78. putsxyf(5,5,">>> Disk error #:%d, File %s",ferror(FP),name);
  79. return(0);
  80. }
  81.  
  82. /* Function for loading any binary file */
  83.  
  84. int bload(char *fname,char *adr,int siz)
  85. {
  86.  if((FP=fopen(fname,"rb"))==0L) return( puterr(fname)) ;
  87.  fread((char *)adr,siz,1,FP);
  88.  fclose(FP);
  89.  return(1);
  90. }
  91.  
  92. /* Function for loading an automaton */
  93.  
  94. int autload(char *aname)
  95. {
  96. int ret;
  97. char name[80];
  98.  
  99. strcpy(name,aname);changext(name,".AUT");
  100. ret=bload(name,(char *) AUTOMATON,2048);
  101. AUTMAX=*AUTOMATON;
  102.  
  103. return(ret);
  104. }
  105.  
  106.  
  107. /* Function for loading a file of sprites into memory.
  108.    This unregistered version allows only 64Kb data  */
  109.  
  110. int sprload(char *spname)
  111. {
  112. int i,res;
  113. unsigned long lvar;
  114. char name[80];
  115.  
  116. strcpy(name,spname);changext(name,".spr");
  117.  
  118. res=bload(name,(char *) SPVECTOR,65520);
  119. if(!res) return(0);
  120.  
  121. SPRMAX=(int) *SPVECTOR;
  122.  
  123. lvar =  (unsigned long) SPVECTOR + SPRMAX * 4 + 8;
  124. SPCODE = (char *) lvar;
  125.  
  126. /* Convert relative addresses to absolute ones */
  127.  
  128. for(i = 1; i <= SPRMAX; i++)
  129. {
  130.  lvar=  *(SPVECTOR+i) + (unsigned long) SPCODE;
  131.  *(SPVECTOR+i)= lvar;
  132. }
  133.  
  134. fclose(FP);
  135. return(1);
  136. }
  137.  
  138. /* Allocate memory for all buffers */
  139.  
  140. int spralloc()
  141. {
  142. unsigned long allowed=0;
  143. unsigned long tobe=2048+6406+65000;
  144.  
  145. AUTOMATON = (int *)   malloc(2048);
  146.  if(AUTOMATON) allowed+=2048;
  147. BLOCKSAVE = (char *)  malloc(6406);
  148.  if(BLOCKSAVE) allowed+=6406;
  149. SPVECTOR  = (unsigned long *) malloc(65000);
  150.  if(SPVECTOR) allowed+=65000;
  151.  
  152. if(allowed == tobe) return;
  153.  
  154. printf("Not enough memory...%lu of %lu",allowed,tobe);
  155. exit(0);
  156. }
  157.  
  158.  
  159.  
  160. void main()
  161. {
  162. int i,j,num,x,y,yold;
  163.  
  164. /* First, allocation of memory buffer */
  165.  
  166. spralloc();
  167.  
  168. /* Loading sprites  */
  169.  
  170. printf("Loading sprites...");
  171. sprload("spdemo1");
  172.  
  173. /* loading automaton */
  174.  
  175. printf("Loading automaton...");
  176. autload("spdemo1");
  177.  
  178.  /* Setting VGA 256 colors screen */
  179.  
  180. setscreen2(VGA18);
  181.  
  182. /* Full screen window for the red background */
  183.  
  184. cls18(4);
  185.  
  186. /* Now, setting window's limits left, top, right, bottom */
  187.  
  188. window18(48,48,591,431);
  189.  
  190. /* Black background inside the windows */
  191.  
  192. cls18(0);
  193.  
  194. /* Animation example */
  195.  
  196. putsxy(2,2,"Testing windows, cls18, sprites and automaton...");
  197.  
  198. i=1;
  199. x=300;
  200. y=20;
  201.  
  202. while(y < 460)
  203. {
  204.  num=autread(i);
  205.  i++;
  206.  if(i > AUTMAX) i=1;
  207.  
  208.  waitrefresh();
  209.  csp256c(x, y, getspadr18(num));
  210.  
  211.  delay(40);        /* to slow down and set all computers same speed */
  212.  if(kbhit()) goto bye;
  213.  y++;
  214.  
  215. }
  216.  
  217. /* Restores DOS text screen and exits */
  218.  
  219. bye:
  220. setscreen2(TEXTMODE);
  221. exit(0);
  222. }
  223.